package com.glory.hikvisionmobile.activity;


import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.LotSkipAndBack.LotBackBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class Lot_LotBackStepActivity extends BaseActivity {

    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvRecipe)
    TextView tvRecipe;
    @BindView(R.id.tvProductDetails)
    TextView tvProductDetails;
    @BindView(R.id.tvLotNumber)
    TextView tvLotNumber;
    @BindView(R.id.tvCurrentStepCode)
    TextView tvCurrentStepCode;
    @BindView(R.id.tvCurrentStepDetails)
    TextView tvCurrentStepDetails;
    @BindView(R.id.tvPreviousStepCode)
    TextView tvPreviousStepCode;
    @BindView(R.id.tvPreviousStepDetails)
    TextView tvPreviousStepDetails;
    @BindView(R.id.tvReplay)
    TextView tvReplay;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvRecipeDesc)
    TextView tvRecipeDesc;
    @BindView(R.id.tvPartName)
    TextView tvPartName;
    @BindView(R.id.etRemarks)
    EditText etRemarks;
    @BindView(R.id.tvProcedureName)
    TextView tvProcedureName;
    private String backUpStepName;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lot_back_step;
    }

    @Override
    protected void initView() {
        initEdit(etLot, s -> initData(s,""));

    }

    @Override
    protected void initData() {}

    private void initData(String lotID,String actionType){
        JsonObject map = new JsonObject();
        map.addProperty("lotId",lotID);
        map.addProperty("actionType",actionType);
        map.addProperty("adtionComment",etRemarks.getText().toString().trim());
        NetUtils.requestDara("HIK.PdaLotBackUp", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<LotBackBean>>(
                new TypeToken<BaseResponseBean<LotBackBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<LotBackBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    if (actionType.equals("BackUp")){
                        showConfirmDialog(Lot_LotBackStepActivity.this,"退步成功");
                        return;
                    }else {
                        LotBackBean lotBackBean = response.body().getBody();
                        tvCurrentStepCode.setText(lotBackBean.getStepName());
                        tvCurrentStepDetails.setText(lotBackBean.getStepDesc());
                        tvLotNumber.setText(lotBackBean.getMainQty());
                        tvProductDetails.setText(lotBackBean.getPartDesc());
                        tvRecipe.setText(lotBackBean.getRecipeName());
                        backUpStepName = lotBackBean.getBackUpStepName();
                        tvPreviousStepCode.setText(backUpStepName);
                        tvPartName.setText(lotBackBean.getPartName());
                        tvRecipeDesc.setText(lotBackBean.getRecipeDesc());
                        tvPreviousStepDetails.setText(lotBackBean.getBackUpStepDesc());
                        tvProcedureName.setText(lotBackBean.getProcedureName());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LotBackBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaLotBackUp");

            }
        });

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("退步");
        freeUI_titleView.setRightListener("刷新", v -> {

            initPermission("Refresh", this, new ActionControlListener() {
                @Override
                public void onControlChangeListener() {
                    if (editIsEmpty(etLot,"批次号不能为空")){
                        initData(etLot.getText().toString().trim(),"");
                    }
                }
            });


        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick(R.id.tvReplay)
    public void onViewClicked() {
        initPermission("Submit", this, new ActionControlListener() {
            @Override
            public void onControlChangeListener() {
                if (!editIsEmpty(etRemarks,"备注不能为空"))
                    return;

                if (editIsEmpty(etLot,"批次号不能为空")){
                    if (FreeApi_StringUtils.isEmpty(backUpStepName)){
                        showConfirmDialog("当前工序无上一道工序，无法退步");
                    }else {
                        DialogUtils.showLoginDialog(Lot_LotBackStepActivity.this, Constant.CURRENT_USER, () ->
                                initData(etLot.getText().toString().trim(),"BackUp"), getSupportFragmentManager());
                    }

                }
            }
        });


    }
}
